﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.Diagnostics;

namespace LightOMator.View
{
    public partial class TimeSpanControl : UserControl
    {

        public static readonly DependencyProperty NumberProperty = DependencyProperty.Register("Number",
                                                    typeof(double),
                                                    typeof(TimeSpanControl),
                                                    new PropertyMetadata(onNumberChanged));

        private static void onNumberChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (e.NewValue != null)
            {
                TimeSpanControl control = (TimeSpanControl)d;
                double newValue = (double)e.NewValue;
                control.adjustUnit(newValue);
                double textBoxValue = newValue / control.getUnitFactor();
                string textBoxString = textBoxValue.ToString();
                if (!textBoxString.Equals(control.NumericInputBox.Text))
                {
                    control._isIgnoreTextChange = true;
                    control.NumericInputBox.Text = textBoxString;
                }
            }
        }


        public double Number
        {
            get { return (double)GetValue(NumberProperty); }
            set { SetValue(NumberProperty, value); }
        }

        private bool _isIgnoreTextChange;

        public TimeSpanControl()
        {
            InitializeComponent();
            UnitPicker.SelectionChanged += UnitPicker_SelectionChanged;
            NumericInputBox.TextChanged += NumericInputBox_TextChanged;
        }

        private int getUnitFactor(int index)
        {
            switch (index)
            {
                case 0: // miliseconds
                    return 1;
                case 1: // seconds
                    return 1000;
                case 2: // minutes
                    return 60 * 1000;
                case 3: // hours
                    return 60 * 60 * 1000;
            }
            throw new InvalidOperationException("for selected index " + UnitPicker.SelectedIndex);
        }

        private int getUnitFactor()
        {
            return getUnitFactor(UnitPicker.SelectedIndex);
        }


        private void adjustUnit(double value)
        {
            int index;
            if (value < 1000)
            {
                index = 0;
            }
            else if (value < 60 * 1000)
            {
                index = 1;
            }
            else if (value < 60 * 60 * 1000)
            {
                index = 2;
            }
            else
            {
                index = 3;
            }
            if (index != UnitPicker.SelectedIndex)
            {
                _isIgnoreTextChange = true;
                UnitPicker.SelectedIndex = index;
            }
        }


        void NumericInputBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (_isIgnoreTextChange)
            {
                _isIgnoreTextChange = false;
                return;
            }
            if (NumericInputBox.Text.Length == 0)
            {
                return;
            }
            int factor = getUnitFactor();
            try
            {
                double value = double.Parse(NumericInputBox.Text);
                Number = value * factor;
            }
            catch (Exception)
            {
                // ignore
            }
        }


        void UnitPicker_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            NumericInputBox_TextChanged(null, null);
            //int factor = getUnitFactor();
            //Number *= factor;
        }


    }
}
